.TH std::holds_alternative 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::holds_alternative \- std::holds_alternative

.SH Synopsis
   Defined in header <variant>
   template< class T, class... Types >
   constexpr bool holds_alternative( const std::variant<Types...>& v )    \fI(since C++17)\fP
   noexcept;

   Checks if the variant v holds the alternative T. The call is ill-formed if T does
   not appear exactly once in Types...

.SH Parameters

   v - variant to examine

.SH Return value

   true if the variant currently holds the alternative T, false otherwise.

.SH Example


// Run this code

 #include <variant>
 #include <string>
 #include <iostream>
 int main()
 {
     std::variant<int, std::string> v = "abc";
     std::cout << std::boolalpha
               << "variant holds int? "
               << std::holds_alternative<int>(v) << '\\n'
               << "variant holds string? "
               << std::holds_alternative<std::string>(v) << '\\n';
 }

.SH Output:

 variant holds int? false
 variant holds string? true

.SH See also

                     returns the zero-based index of the alternative held by the
   index             variant
                     \fI(public member function)\fP
   get(std::variant) reads the value of the variant given the index or the type (if the
   \fI(C++17)\fP           type is unique), throws on error
                     \fI(function template)\fP
